﻿using System.Data.Common;
using System.Data;
using ESRI.ArcGIS.Geodatabase;
using NHibernate.Driver;
using System.Collections.Generic;

namespace Inovout.Gis.Data
{
    public class GisCommand : DbCommand
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(GisCommand));

        private DbConnection _connection;
        private DbTransaction _transaction;
        private DbCommand rmdbCommand;
        private DriverBase rmdbDrive;
        public GisCommand(DriverBase rmdbDrive)
        {
            this.rmdbDrive = rmdbDrive;
            this.rmdbCommand = (DbCommand)rmdbDrive.CreateCommand();
        }
        public override void Cancel()
        {
            rmdbCommand.Cancel();
        }

        public override string CommandText
        {
            get { return rmdbCommand.CommandText; }
            set { rmdbCommand.CommandText = value; }
        }

        public override int CommandTimeout
        {
            get { return rmdbCommand.CommandTimeout; }
            set { rmdbCommand.CommandTimeout = value; }
        }

        public override System.Data.CommandType CommandType
        {
            get { return rmdbCommand.CommandType; }
            set { rmdbCommand.CommandType = value; }
        }

        protected override DbParameter CreateDbParameter()
        {
            return (DbParameter)rmdbCommand.CreateParameter();
        }

        protected override DbConnection DbConnection
        {
            get { return _connection; }
            set
            {
                _connection = (DbConnection)value;
                rmdbCommand.Connection = _connection;
            }
        }

        protected override DbParameterCollection DbParameterCollection
        {
            get { return (DbParameterCollection)rmdbCommand.Parameters; }
        }

        protected override DbTransaction DbTransaction
        {
            get { return _transaction; }
            set
            {
                if (value == null)
                {
                    _transaction = null;
                    rmdbCommand.Transaction = null;
                }
                else
                {
                    _transaction = (DbTransaction)value;
                    rmdbCommand.Transaction = _transaction;
                }
            }
        }

        public override bool DesignTimeVisible
        {
            get { return rmdbCommand.DesignTimeVisible; }
            set { rmdbCommand.DesignTimeVisible = value; }
        }

        protected override DbDataReader ExecuteDbDataReader(System.Data.CommandBehavior behavior)
        {
            return (DbDataReader)rmdbCommand.ExecuteReader(behavior);
        }

        public override int ExecuteNonQuery()
        {
            #region 判断是哪个数据库
            //if ((rmdbDrive is SqlClientDriver))
            //{//是sql

            //}
            //else if ((rmdbDrive is OracleClientDriver) || (rmdbDrive is OracleDataClientDriver))
            //{//是oracle

            //}
            //else if (rmdbDrive is MySqlDataDriver)
            //{//是MySql 
            //}
            //else if ((rmdbDrive is ASAClientDriver) || (rmdbDrive is ASA10ClientDriver))
            //{//是Sybase

            //}
            //else if ((rmdbDrive is NHibernate.JetDriver.JetDriver) )
            //{//是access

            //}
            #endregion

            //判断是不是INSERT
            if (this.CommandText.StartsWith("INSERT"))
            {
                if (this.Connection.State == ConnectionState.Connecting)
                {
                    IWorkspace workspace;

                    string tableName = null;//
                    return 1;
                }
                else if (true)
                {
                    return rmdbCommand.ExecuteNonQuery();
                }
            }
            return 0;
        }

        public override object ExecuteScalar()
        {
            return rmdbCommand.ExecuteScalar();
        }

        public override void Prepare()
        {
            rmdbCommand.Prepare();
        }

        public override System.Data.UpdateRowSource UpdatedRowSource
        {
            get { return rmdbCommand.UpdatedRowSource; }
            set { rmdbCommand.UpdatedRowSource = value; }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                rmdbCommand.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}
